package de.tudarmstadt.ukp.muip.fibqdojo.rule;

import de.tudarmstadt.ukp.muip.fibqdojo.interfaces.IFIBQRule;
import de.tudarmstadt.ukp.muip.fibqdojo.interfaces.IFIBQSentence;
import de.tudarmstadt.ukp.muip.fibqdojo.interfaces.IFIBQSentenceToken;
import de.tudarmstadt.ukp.muip.fibqdojo.visitor.GetSentenceTokenListVisitor;

public class SentenceLengthMatchingRule implements IFIBQRule {

    private final int maxLength;
    private final int minLength;

    public SentenceLengthMatchingRule(int minLength, int maxLength) {
        validateRange(minLength, maxLength);
        this.minLength = minLength;
        this.maxLength = maxLength;
    }

    private void validateRange(int minLength, int maxLength) {
        String errorText = "Range mismatch, parameters have to be > 0 and minLength <= maxLength";
        if (minLength < 0) {
            throw new IllegalArgumentException(errorText);
        }
        if (maxLength < 0) {
            throw new IllegalArgumentException(errorText);
        }
        if (maxLength < minLength) {
            throw new IllegalArgumentException(errorText);
        }
    }

    @Override
    public boolean match(IFIBQSentence sentence) {
        GetSentenceTokenListVisitor visitor = new GetSentenceTokenListVisitor();
        sentence.accept(visitor);
        int numberOfTokens = visitor.getSentenceTokens().size();
        return !(numberOfTokens >= minLength && numberOfTokens <= maxLength);
    }

    @Override
    public boolean match(IFIBQSentenceToken sentenceToken) {
        return false;
    }

}
